package com.example.teaculture.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import androidx.annotation.Nullable;

import com.example.teaculture.R;
import com.example.teaculture.activity.FavorDetailActivity;
import com.example.teaculture.adapter.HeadlineBaseAdapter;
import com.example.teaculture.adapter.KnowledgeBaseAdapter;
import com.example.teaculture.adapter.TeaBaseAdapter;
import com.example.teaculture.application.MyApplication;
import com.example.teaculture.model.Favor;
import com.example.teaculture.model.Headline;
import com.example.teaculture.model.TeaInfo;
import com.example.teaculture.model.Teaknown;
import com.example.teaculture.view.RefreshableView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.List;

public class EncyclopediaFragment extends androidx.fragment.app.Fragment {

    RefreshableView refreshableView;

    Handler handler;

    List<TeaInfo> encyclopedias;

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.encyclopedia_fragment, container, false);
        ListView lv_view = view.findViewById(R.id.list_view);


        encyclopedias = MyApplication.getInstance().encyclopedias;
        TeaBaseAdapter baseAdapter = new TeaBaseAdapter(getContext(), encyclopedias);
        lv_view.setAdapter(baseAdapter);

        handler = new Handler(message -> {
            if (message.what == 0) {

                TeaBaseAdapter baseAdapter1 = new TeaBaseAdapter(getContext(), encyclopedias);
                lv_view.setAdapter(baseAdapter1);

            }
            return false;
        });

        lv_view.setOnItemClickListener((adapterView, view1, i, l) -> {
            Favor favor = new Favor();
            favor.setTitle(encyclopedias.get(i).getTitle());
            favor.setUrl(encyclopedias.get(i).getUrl());
            MyApplication.getInstance().favor = favor;
            Intent intent = new Intent(getContext(), FavorDetailActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            intent.putExtra("url", encyclopedias.get(i).getUrl());
            intent.putExtra("title", encyclopedias.get(i).getTitle());
            startActivity(intent);
        });

        refreshableView = (RefreshableView) view.findViewById(R.id.refreshable_view);
        refreshableView.setOnRefreshListener(new RefreshableView.PullToRefreshListener() {
            @Override
            public void onRefresh() {
                try {
                    encyclopedias.clear();
                    Document doc = Jsoup.connect("https://chaaowang.com/fenlei/changshi/page/1").get();
                    Elements elements = doc.select("li.item");
                    for (Element element : elements) {
                        String title = element.select("div.item-img").select("a.item-img-inner").attr("title");
                        String info = element.select("div.item-content").select("div.item-excerpt").select("p").text();
                        String image = element.select("img.j-lazy").attr("data-original");
                        String url = element.select("div.item-img").select("a.item-img-inner").attr("href");
                        String date = element.select("div.item-content").select("div.item-meta").select("span.item-meta-li.date").text();
                        String newDate = date.replace("年", "-").replace("月", "-").replace("日", "");
                        TeaInfo teaInfo = new TeaInfo(title, info, url, newDate, image);
                        encyclopedias.add(teaInfo);
                    }
                    Message message = Message.obtain();
                    message.what = 0;
                    handler.sendMessage(message);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                refreshableView.finishRefreshing();
            }
        }, 2);

        return view;
    }
}
